package jihe.map;

import org.junit.Test;

import java.util.*;

/**
 * Created by leiylong on 2020/5/30
 */
public class map {
    /**
     * 无序，唯一，线程不安全
     */
    @Test
    public void hashMap() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("张三", "小明");
        hashMap.put("张慢", "小明");
        hashMap.put("张三", "小明");
        Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//           System.out.println(iterator.next());
        }

    }

    /**
     * 有序，唯一
     */
    @Test
    public void treeMap() {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("张三", "小明");
        treeMap.put("张ji", "小明");
        treeMap.put("张三", "小明");
        Iterator<Map.Entry<String, String>> iterator = treeMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

        }
    }
    /**
     * 有序，唯一
     */
    @Test
    public void linkedHashMap() {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("张三", "小明");
        linkedHashMap.put("张慢", "小明");
        linkedHashMap.put("张三", "小明");
        //迭代器遍历
        System.out.println("//迭代器遍历");
        Iterator<Map.Entry<String, String>> iterator = linkedHashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

        }
       //for循环遍历
        System.out.println("//for循环遍历");
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        //增强for循环遍历
        System.out.println("//增强for循环遍历");
        for (String key : linkedHashMap.keySet()) {
            String value = linkedHashMap.get(key);
            System.out.println("Key = " + key + ", Value = " + value);
        }
    }
}
